/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sv.com.intesal.rrhh.controlador;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import sv.com.intesal.clases.abstracts.ControladorAbstract;
import sv.com.intesal.rrhh.bean.Area;
import sv.com.intesal.rrhh.sql.SQLArea;

/**
 *
 * @author ejerez
 */
public class AreaControlador extends ControladorAbstract {

    public void insert(Area a) throws Exception {
        try {
            super.setPreparedStatement(SQLArea.insert);
            super.ps.setString(1, a.getNombre_a());
            super.ps.setString(2, super.v.getEstado(a.getEstado_filtro_a()));
            super.ps.executeUpdate();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    public void update(Area a) throws Exception {
        try {
            super.setPreparedStatement(SQLArea.update);
            super.ps.setString(1, a.getNombre_a());
            super.ps.setString(2, super.v.getEstado(a.getEstado_filtro_a()));
            super.ps.setInt(3, a.getCodigo_a());
            super.ps.executeUpdate();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    public List<Area> getAreas() throws Exception {
        List<Area> lista = new ArrayList<Area>();
        try {
            this.getExecuteQuery(SQLArea.getAreas);
            while (super.rs.next()) {
                lista.add(this.getArea(super.rs));
            }
            return lista;
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    public List<Area> getAreasActivos() throws Exception {
        List<Area> lista = new ArrayList<Area>();
        try {
            this.getExecuteQuery(SQLArea.getAreaActivos);
            while (super.rs.next()) {
                lista.add(this.getArea(super.rs));
            }
            return lista;
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    private Area getArea(ResultSet rs) throws Exception {
        try {
            Area a = new Area();
            a.setCodigo_a(rs.getInt("codigo_a"));
            a.setNombre_a(rs.getString("nombre_a"));
            a.setEstado_a(rs.getString("estado_a"));
            a.setEstado_filtro_a(v.getEstado(rs.getString("estado_a")));
            return a;
        } catch (Exception e) {
            throw new Exception(e);
        }
    }
}
